<?php
if (!defined("MCTDESIGN")) die("# ");
function get_parent_names($cat_id, &$str) {

	global $mysql, $tb_fix, $stop, $dir_tpl;

	$query = $mysql->query("SELECT cat_parent,cat_id,cat_title FROM ".$tb_fix."_category WHERE cat_id='$cat_id' ORDER BY cat_order ASC");

	while ($result = $mysql->fetch_array($query)){

		if ( $cat_id == $result['cat_id'] ){

			get_parent_names($result['cat_parent'],  $str);
            $caturl = convertText($result['cat_title']);
			$str .= ' <img src="'.$dir_tpl.'/images/product/muiten.png">  <a class="active" href="'.$caturl.'/'.$result['cat_id'].'/index1.html">'. $result['cat_title'] .'</a>';

		}

	}

}
function shopBrowse($cat)

{

	global $mysql,$dir_tpl,$tb_fix,$page,$temp;

	$page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;

	$s = isset($_GET["s"]) ? m_htmlchars($_GET["s"]) : "desc";
    // sub category
    $q_cat = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='$cat' ORDER BY cat_order ASC");
    $list_category = "";
	while ($r_cat = $mysql->fetch_array($q_cat)) {
        $caturl = convertText($r_cat['cat_title']);
		$list_category .= "<li><a href=\"$caturl/".$r_cat['cat_id']."/index1.html\">  » ". $r_cat['cat_title'] ."</a></li>";

	}
    $temp->assign( 'list_category', "$list_category" );
    // show san pham
	$_limit = 15;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_id='$cat' ORDER BY cat_order ASC LIMIT 1");

	$t = $mysql->num_rows($q);

	if ($t>0) {


		$temp->assign( 'base_tpl', "".$dir_tpl."" );
		# BEGIN : INFO CAT
		$r = $mysql->fetch_array($q);
		get_parent_names($r["cat_id"], $parent_name);
		$temp->assign( 'parent_name', "$parent_name" );
        $temp->assign("cat", $r );    
		# END : INFO CAT
      
		$q = $mysql->query("SELECT p.*,c.cat_code,c.cat_title,c.cat_id FROM ".$tb_fix."_product AS p,".$tb_fix."_category AS c 
                            WHERE p.pro_catid=c.cat_id AND (p.pro_catid='$cat' OR p.pro_catpid='$cat' OR p.pro_catrid='$cat') 
                            ORDER BY p.pro_id DESC ");
                                    
		$t = $mysql->num_rows($q);
        
		if ($t>0) {

			$_start = ($page-1) * $_limit ;

			$_end   = $page*$_limit;

			$num_pages = ceil($t/$_limit) ;

			if ($page > $num_pages) $page=$num_pages;

			if ($page < 1 ) $page=1;

			$page_view = Paginate(show_cat_code($cat),$cat,$t, $_limit, $page);

			$c=0;

			$i = 0;

			$c_td = 0;

			$list_product = "";

			while ($r = $mysql->fetch_array($q)) {

				if ($i>=$_start) {

					$c++;

					$list_product .= show_category_product($r,$catid="",$catparentid="");

				}

				$i++;

				if ($i>=$_end) break;

			}

			$temp->assign( 'page_view', "".$page_view."" );

			$temp->assign( 'list_product', "".$list_product."" );

		}

		else $temp->assign( 'list_product', "<br><br><center>Danh mục này chưa có sản phẩm nào.</center><br><br>" );

		$temp->assign( 'shop_baogia', "".$shop_baogia."" );

		$temp->assign( 'base_tpl', "".$dir_tpl."" );

		$html_browse = $temp->fetch( 'product/_shop_browse.mct' );

	}

	else $html_browse = msg_Error("<br><br><br>Không tìm thấy sản phẩm nào.");

	return $html_browse;

}



function productdetails($id,$catid) {

	global $mysql,$dir_tpl,$tb_fix,$page, $shop_usd, $temp,$anyDB;
	$anyDB->query("UPDATE ".$tb_fix."_product SET pro_counter=pro_counter+1 WHERE pro_id='".$id."'");

	

	$q = $anyDB->query("SELECT * FROM ".$tb_fix."_product AS p
                        JOIN ".$tb_fix."_category AS c
                            ON p.pro_catid=c.cat_id
                        LEFT JOIN ".$tb_fix."_manufacturer AS m 
                            ON p.pro_manufacturerid=m.manu_id
                        WHERE p.pro_id='".$id."'
                             LIMIT 1");
                             

	$t = $anyDB->numRows($q);

	if ($t>0)

		{

			$temp_detail = $temp;
			$temp_detail->assign( 'base_tpl', "".$dir_tpl."" );
			$r = $anyDB->getNext($q);
            // breakcum
			get_parent_names($r["pro_catid"], $parent_name);
			$temp_detail->assign( 'parent_name', "$parent_name" );
            // sucurity
			$security=rand(111111,139999);
			$temp_detail->assign( 'security', "".$security."" );
            // xuat xu 
            $r['pro_manufacturer'] = $r["manu_title"];
            // gia san pham
			if ($r["pro_promotion_vnd"]!=0) {
				$price = $r["pro_promotion_vnd"];
				$price = number_format($price, 0, '.', '.');
				$html_price = "<font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price." VNĐ</font>";
			}

			else {
				$price = $r["pro_price"]*$shop_usd;
				$price = number_format($price, 0, '.', '.');
				$html_price = "<font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price." VNĐ</font>";
				if ($r["pro_promotion_usd"]!=0) {
					$price_promotion = $r["pro_promotion_usd"]*$shop_usd;
					$price_promotion = number_format($price_promotion, 0, '.', '.');
					$html_price = "<font style=\"color:#666666; font-size: 11px; text-decoration: line-through;\">".$price." VNĐ</font><br><font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price_promotion." VNĐ</font>";
				}
			}
            $r['pro_price'] = $html_price;
            //chitiet
            $r['pro_content'] = htmlchars($r["pro_content"]);
            $r['pro_promotion_content'] = htmlchars($r["pro_promotion_content"]);
            // hinh anh
			/*$size = getimagesize($r["pro_images"]);
			if ($size[0]>230) $pro_images = "viewImage.php?image=".$r["pro_images"]."&width=230";
			else $pro_images = $r["pro_images"];
            $r['pro_img'] = $pro_images;*/
            $r['pro_img_small'] = "viewImage.php?image=".$r["pro_images"]."&width=190&height=100";
            // pro url
            $r['pro_code'] = convertText($r['pro_name']);
            $temp_detail->assign("pro", $r);
            
            
			//SAN PHAM KHAC
			$q_other = $anyDB->query("SELECT p.*,c.cat_title FROM ".$tb_fix."_product AS p,".$tb_fix."_category AS c WHERE p.pro_catid=c.cat_id AND (p.pro_catid='".$r["pro_catid"]."' OR p.pro_catpid='".$r["pro_catid"]."' OR p.pro_catrid='".$r["pro_catid"]."') AND p.pro_id!='".$r["pro_id"]."' ORDER BY pro_id DESC LIMIT 9");
            $q_t = $anyDB->numRows($q_other);
            if($q_t > 0){
                $other = $anyDB->getAll($q_other);
                foreach($other as $key => $value){
                    $other[$key]['cat_code'] = convertText($value['cat_title']);
                    $other[$key]['pro_code'] = convertText($value['pro_name']);
                    $other[$key]['pro_content_desc'] = subCutString($value['pro_content_desc'],15);
                    // hinh
                    if($value['pro_images']!= ""){
                        $other[$key]['pro_images'] = "viewImage.php?image=".$value["pro_images"]."&width=100&height=75";
                    }else{
                        $other[$key]['pro_images'] = "viewImage.php?image=_uploads/product/no_image.jpg&width=100&height=75";
                    }
                    // gia sp
                    if ($value["pro_promotion_vnd"]!=0) {

        					$price = $value["pro_promotion_vnd"];
        
        					$price = number_format($price, 0, '.', '.');
        
        					$other[$key]['pro_price'] = "<br><font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price." VNĐ</font>";
        
        				}
        
        			else {
        
        					$price = $value["pro_price"]*$shop_usd;
        
        					$price = number_format($price, 0, '.', '.');
        
        					$other[$key]['pro_price'] = "<br><font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price." VNĐ</font>";
        
        					if ($value["pro_promotion_usd"]!=0) {
        
        						$price_promotion = $value["pro_promotion_usd"]*$shop_usd;
        
        						$price_promotion = number_format($price_promotion, 0, '.', '.');
        
        						$other[$key]['pro_price'] = "<font style=\"color:#666666; font-size: 11px; text-decoration: line-through;\">".$price." VNĐ</font><br><font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price_promotion." VNĐ</font>";;
        
        				}        
        			}
                }
            }
            //echo "<pre>";print_r($other);die();
            $temp_detail->assign("pro_o", $other);
			//Other product

			$html_detail = $temp_detail->fetch( 'product/_product_detail.mct' );

	}

	return $html_detail;

}



function productOrder($id,$procode) {

	global $mysql,$dir_tpl,$tb_fix,$cart_session,$shop_usd;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_product AS p,".$tb_fix."_category AS c WHERE p.pro_catid=c.cat_id AND p.pro_id='$id' ORDER BY pro_id DESC");

	$t = $mysql->num_rows($q);

	if ($t>0) {

		$html = "";

		$list_cart = $cart_session;

		$r = $mysql->fetch_array($q);

		//$price = number_format($r["pro_price"], 0, '.', '.');

		if ($r["pro_promotion_vnd"]!=0) {

			$price = $r["pro_promotion_vnd"];

		}

		else {

			$price = $r["pro_price"]*$shop_usd;

			if ($r["pro_promotion_usd"]!=0) {

				$price = $r["pro_promotion_usd"]*$shop_usd;

			}

		}

		unset($carts);

		$have_been =  false;

		$carts = explode("**",$cart_session);

		for($i = 0;$i < count($carts);$i++) {

			list($proid, $proname)=explode("||",$carts[$i]);

			if ($proid == $r["pro_id"] ) $have_been = true;

		}

		if ($have_been) {

			$html = "<script>alert('Đã có sản phẩm này trong giỏ hàng, bạn vui lòng kiểm tra lại.');window.location=\"GioHang.html\";</script>";

		}

		else {

			$list_cart .= "**".$r["pro_id"]."||".$r["pro_name"]."||".$price."||1||".$r["pro_firm"]."||".$r["pro_warranty"]."";//pro_firm

			$_SESSION["cart_session"] = $list_cart;

            header("Location: GioHang.html");

		}

	}

	else {

		$html = msg_Error("<br><br><br>Không tồn tại sản phẩm này.");

	}

	return $html;

}



switch($act){
	case "browse":
		$content = shopBrowse($catid);
		break;
	case "details":
        $title = get_title_product($proid);
		$content = productdetails($proid,$catid);
		break;
	case "order":
		$content = productOrder($proid,$procode);
		break;
}

?>